package wangjg.xuexiban.widget.file;

import java.io.File;

import wangjg.xuexiban.R;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class FileSelectorActivity extends ListActivity {
	private TextView currentPathView;
	private static final String ROOT_PATH = "/";
	private static String curPath = "/";
	
	private FileSelectorAdapter fileSelectorAdapter ;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.fileselector_list);
		this.setWin();
		currentPathView = (TextView) findViewById(R.id.currentPath);
		Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
		buttonConfirm.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				callback();
				finish();
			}
		});
		Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
		buttonCancle.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		Intent intent = getIntent();
		String path = intent.getStringExtra("path");
		if(path!=null){
			File file = new File(path);
			if(!file.exists()){
				path = ROOT_PATH;
			}else if(file.isFile()){
				path = file.getParent();
			}
			this.curPath = path;
		}
		if(curPath!=null){
			File file = new File(curPath);
			if(file.canRead()){
				if(file.isFile()){
					curPath = file.getParent();
				}
				this.showDir(getCurrentPath());
			}else{
				showDir(ROOT_PATH);
			}
		}else{
			showDir(ROOT_PATH);
		}
	}
	
	private void setWin(){
		DisplayMetrics dm = new DisplayMetrics();   
		getWindowManager().getDefaultDisplay().getMetrics(dm);   
		int width = (int) (dm.widthPixels*0.84);
		int heigth = (int) (dm.heightPixels*0.8);
		LinearLayout ll = (LinearLayout)findViewById(R.id.win);
		ll.setLayoutParams(new ScrollView.LayoutParams(width, heigth));
		Log.e("ccc",ll.getLayoutParams().getClass().getName());
	}
	
	private void callback(){
		ComponentName cn = this.getCallingActivity();
		Intent intent = new Intent();
		intent.setComponent(cn);
		Bundle bundle = new Bundle();
		bundle.putString("path", getCurrentPath());
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		finish();
	}

	protected void onListItemClick(ListView listView, View item, int itemPos, long itemRowId) {
		File file = (File) fileSelectorAdapter.getItem(itemPos);
		if (file.isDirectory()) {
			showDir(file);
		}else{
			this.saveCurrentPath(file.getPath());
			this.callback();
		}
	}
	
	private void saveCurrentPath(String path){
		this.curPath = path;
	}
	
	private String getCurrentPath(){
		return this.curPath;
	}
	
	private void showDir(String path) {
		showDir(new File(path));
	}
	
	private void showDir(File file){
		if(!file.canRead()){
			Toast toast=Toast.makeText(this, "没有权限", Toast.LENGTH_SHORT);
			toast.show();
			return;
		}
		fileSelectorAdapter = new FileSelectorAdapter(this, file);
		this.setListAdapter(fileSelectorAdapter);
		this.currentPathView.setText(file.getPath());
		this.saveCurrentPath(file.getPath());
	}
}
